Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1380: Validate python parameters passed to SP compute method #1385

Merged
merged 3 commits into from
Jan 16, 2018

Conversation

lscheinkman
Copy link
Contributor

@lscheinkman lscheinkman commented Jan 12, 2018

Fixes #1380
The swig interface now validates the numpy.array type making sure it matches the C++ implementation (nupic::UInt). It will now throw an exception when the compute method is called with the wrong data type.

NTA_THROW << "Invalid data type given for active array."
<< " Expecting 'uint" << sizeof(nupic::UInt) * 8 << "'"
<< " but got '" << PyArray_DTYPE(y)->type << "'";
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth creating a CheckedNumpyVectorWeakRefT by copy-pasting the NumpyVectorWeakRefT in https://github.com/numenta/nupic.core/blob/91b45451ec77aabbf71ee9f3645ec704532664f1/src/nupic/py_support/NumpyVector.hpp#L411 and changing it to do checks in release builds. Then we can reuse it for any dtype. The existing code would check that the dtype is correct (which, strictly speaking, this code doesn't currently do). I like that you throw useful error text -- we'd want that in the Checked class.

This we could change this code to

nupic::CheckedNumpyVectorWeakRefT<nupic::UInt32> inputArray(py_inputArray);
nupic::CheckedNumpyVectorWeakRefT<nupic::UInt32> activeArray(py_activeArray);
self->compute(inputArray.begin(), learn, activeArray.begin());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrcslws regarding

The existing code would check that the dtype is correct (which, strictly speaking, this code doesn't currently do)

Can you give an example where this code does not check the dtype of the numpy array passed to the function? I would like to cover that condition with a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrcslws Why create the new CheckedNumpyVectorWeakRefT class?
NumpyVectorWeakRefT seems to do what I need.
Any reason why this class should only throw an exception in debug mode?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally used NTA_ASSERT in the NumpyVectorWeakRefT. In the SparseMatrix, the parameters are checked in the Python bindings. It would be wasteful to recheck them. The main purpose of the class isn't to check its parameters, it's to expose the array data.

Copy link
Member

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vector<UInt> out2(nColumns, 0);
sp.compute(input.data(), false, out1.data());
sp.compute(input.data(), false, out2.data());
EXPECT_EQ(out1, out2);
Copy link
Member

@breznak breznak Jan 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test validates the issue. For 2 reasons:

  • your commit eba72d0 (this test) passed OK even without the suggested fix
  • the fix is in python related .i file. The issue exhibits even in pure c++
  • please see Fix SP unstable output, synapse decay #1382 for discussion and example test that crashes correctly

@breznak
Copy link
Member

breznak commented Jan 12, 2018

Re-reading the PR, I think the py fix is fine 👍 , but the test should be removed (checks something else and does not reproduce it), instead, make test that validates this fix (python compute called with numpy array where dtype does not match UInt)

Copy link
Contributor

@mrcslws mrcslws left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@rhyolight
Copy link
Member

@lscheinkman @mrcslws This is clear to merge. I have warned the community of potential problems.

@lscheinkman lscheinkman merged commit c33a6e8 into numenta:master Jan 16, 2018
@lscheinkman lscheinkman deleted the 1380 branch January 16, 2018 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants